package clearjerk.pic.model;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Stack;
/*
 * This implementation was just an experiment and is not currently being used. It works by loading 
 * all of the potential image steps into a single stack at the beginning of the application,
 * rather than creating them as needed.
 * 
 */
public class ImageModNew {
	private BufferedImage image;
	private Color[][] orig;
	private Stack<Color[][]> steps;
	private int width;
	private int height;
	public ImageModNew(){
		image = null;
		width = 0;
		height = 0;
		steps = new Stack <Color[][]>();
		orig = new Color[0][0];
		steps.push(orig);
	}
	public ImageModNew(BufferedImage im){
		steps = new Stack <Color[][]>();
		image= im;
		width = image.getWidth();
		height = image.getHeight();
		orig = new Color[width][height];
		if(width%2 != 0)
			width=width-1;
		if(height%2 !=0)
			height = height-1;
		for(int x = 0; x< width; x++)
			for(int y = 0; y< height; y++){
				orig[x][y]=new Color(image.getRGB(x,y));
			}
		steps.push(orig);
		fillMap();
	}
	public int getSideLength(int x) {
		return 2*x-1;
	}
	public void fillMap(){
		int count = 1;

		Color[][] temp;
		while(true){
			Color[][] last = steps.peek();
			int newWidth = last.length/2;
			int newHeight = last.length/2;
			temp = new Color[newHeight][newWidth];
			for(int x = 0; x < newWidth*2; x+=2)
				for(int y = 0; y< newHeight*2; y+=2){
					int r = last[x][y].getRed() + last[x+1][y].getRed() + last[x][y+1].getRed() + last[x+1][y+1].getRed();
					int g = last[x][y].getGreen() +last[x+1][y].getGreen() + last[x][y+1].getGreen() + last[x+1][y+1].getGreen(); 
					int b = last[x][y].getBlue() +last[x+1][y].getBlue() +last[x][y+1].getBlue() +last[x+1][y+1].getBlue();
					temp[x/2][y/2] = new Color((int)Math.round(r/4.0),(int)Math.round(g/4.0), (int)Math.round(b/4.0));
				}
			count++;
			steps.push(temp);
			if(temp.length<1){
				steps.pop();
				return;
			}
		}
	}
	public Color[][] getNextStep(){
		return steps.pop();
	}
}



